<?php
     function getParameter($pname){
          return isset($_POST[$pname])?$_POST[$pname]:"";
     }

     $code = "abcd1234";//商家密碼
     $merchantnumber = getParameter('merchantnumber');
     $ordernumber = getParameter('ordernumber');
     $amount = getParameter('amount');
     $paymenttype = getParameter('paymenttype');
     $bankid = getParameter('bankid');
     $tohash = $merchantnumber.$code.$amount.$ordernumber;
     $hash = md5($tohash);
     $returnvalue = (getParameter('returnvalue')=="1");
     
     if($returnvalue)
     {
          $adminurl = "https://maple2.neweb.com.tw:443/CashSystemFrontEnd/Query";
          $operation = "regetorder";
          $postdata = "merchantnumber=".$merchantnumber."&ordernumber=".$ordernumber."&amount=".$amount.
                      "&paymenttype=".$paymenttype."&bankid=".$bankid.
                      "&operation=".$operation."&returnvalue=".($returnvalue?"1":"0")."&hash=".$hash;

          $url = parse_url($adminurl);

          $postdatalen = strlen($postdata);
          $postdata = "POST ".$url['path']." HTTP/1.0\r\n".
                     "Content-Type: application/x-www-form-urlencoded\r\n".
                     "Host: ".$url['host'].":".$url['port']."\r\n".
                     "Content-Length: ".$postdatalen."\r\n".
                     "\r\n".
                     $postdata;
          
          $receivedata = "";

////這裡的ssl連接失敗，不曉得要怎麼做才能正確連接？
//$fp = fsockopen ("sslv3://".$url['host'], $url['port'], $errno, $errstr, 90);
//$fp = fsockopen ('ssl://' . $url['host'], $url['port'], $errno, $errstr, 90);
			$fp = fsockopen ($url['host'], '80', $errno, $errstr, 90);
			
          if(!$fp)
          { 
               echo "$errstr ($errno)<br>\n";
          }
          else
          {
               fputs ($fp, $postdata);
               do{ 
                    if(feof($fp))
                    {
                      	break;
                    }
                    $tmpstr = fgets($fp,128);
                    $receivedata = $receivedata.$tmpstr;
               }while(true); //!($tmpstr=="0")
               fclose ($fp);
          }

          $receivedata = str_replace("\r","",trim($receivedata));

          ////將回傳的資料分隔出header和body。
          $isbody = false;
          $httpcode = null;
          $httpmessage = null;
          $result = "";
          $array1 = explode("\n",$receivedata);
          for($i=0;$i<count($array1);$i++)
          {
               if($i==0)
               {
                    $array2 = explode(" ",$array1[$i]);
                    $httpcode = $array2[1];
                    $httpmessage = $array2[2];
               }
               elseif(!$isbody)
               {
                    if(strlen($array1[$i])==0) $isbody = true;
               }
               else
               {
                    $result = $result.$array1[$i];
               }
          }

          if($httpcode != "200")
          {
               if($httpcode=="404") echo "網址錯誤，無法找到網頁!";
               else if($httpcode=="500") echo "伺服器錯誤!";
               else echo $httpmessage;
               return;
          }

          echo "received==>[".$result."]";

////將查詢之後回傳的結果解析成陣列。
//$ary = array();
//parse_str($result, $ary);
//var_dump($ary);
          
          return;
     }
?>
<html>
<head>
<script>
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>繳費資訊重新產生頁</title>
</head>
<body>
<form method="POST" action="http://maple2.neweb.com.tw/CashSystemFrontEnd/Query">
<center>
<table border=1 cellpadding=0 cellspacing=0 bordercolor="#dddddd" width="45%">
     <tr>
          <td>商家編號</td>
          <td><input type="hidden" name="merchantnumber" value="<?php echo $merchantnumber ?>"><?php echo $merchantnumber ?></td>
     </tr>
     <tr>
          <td>訂單編號</td>
          <td><input type="hidden" name="ordernumber" value="<?php echo $ordernumber ?>"><?php echo $ordernumber ?></td>
     </tr>
     <tr>
          <td>金額</td>
          <td><input type="hidden" name="amount" value="<?php echo $amount ?>"><?php echo $amount ?></td>
     </tr>
     <tr>
          <td>付款方式</td>
          <td><input type="hidden" name="paymenttype" value="<?php echo $paymenttype ?>"><?php echo $paymenttype ?></td>
     </tr>
     <tr>
          <td>銀行代號</td>
          <td><input type="hidden" name="bankid" value="<?php echo $bankid ?>"><?php echo $bankid ?></td>
     </tr>
     <tr>
          <td colspan=2>
               <input type="hidden" name="operation" value="regetorder">
               <input type="hidden" name="returnvalue" value="<?php echo ($returnvalue?"1":"0") ?>">
               <input type="hidden" name="hash" value="<?php echo $hash ?>">
               <input type="hidden" name="nexturl" value="http://mox.chibakuma.net/PHP5.3/shop.html">
               <input type="submit" value=" 送 出 ">
          </td>
     </tr>
</table>
</center>
</form>
</body>
</html>
